package org.example.back.controller.advice;

import java.time.LocalDate;

import org.example.back.service.advice.UserSportAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

@RestController
@RequestMapping("/userSport/advice")
public class UserSportAdviceController {

    @Autowired
    private UserSportAdviceService userSportAdviceService;

    /**
     * 获取日运动建议（默认昨天），从 session 中取 userId
     */
    @GetMapping("/day")
    public String getDailySportAdvice(@RequestParam(required = false) String date,
                                      HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        LocalDate localDate = (date != null && !date.isEmpty()) ? LocalDate.parse(date) : LocalDate.now().minusDays(1);
        return userSportAdviceService.getDailyAdvice(userId, localDate);
    }

    /**
     * 获取周运动建议（最近7天），从 session 中取 userId
     */
    @GetMapping("/week")
    public String getWeeklySportAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        return userSportAdviceService.getWeeklyAdvice(userId);
    }

    /**
     * 获取月运动建议（最近1个月，按周聚合），从 session 中取 userId
     */
    @GetMapping("/month")
    public String getMonthlySportAdvice(HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        if (userId == null) {
            return "请先登录";
        }
        return userSportAdviceService.getMonthlyAdvice(userId);
    }
}
